﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using EasyHome.Models;
using EasyHome.DAL;

namespace EasyHome.Controllers
{
    public class TablaAdicionalController : Controller
    {
        private EasyHomeContext db = new EasyHomeContext();

        #region GET: /Index
        public ActionResult Index()
        {
            var tablaadicionals = db.TablaAdicionals.Include(t => t.TablaDato);
            return View(tablaadicionals.ToList());
        }
        #endregion

        #region GET: /TablaAdicional/Details
        public ActionResult Details(Guid? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            TablaAdicional tablaadicional = db.TablaAdicionals.Find(id);
            if (tablaadicional == null)
            {
                return HttpNotFound();
            }
            return View(tablaadicional);
        }
        #endregion
        
        #region GET: /TablaAdicional/Create
        public ActionResult Create(Guid? id)
        {
            ViewBag.TablaDatoId = new SelectList(db.TablasDatos, "id", "Descripcion", id);
            return PartialView("Create");
        }
        #endregion

        #region POST: /TablaAdicional/Create
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include = "id,TablaDatoId,Descripcion,Numero,AdicionalString,AdicionalDecimal,AdicionalBoolean,AdicionalDate")] TablaAdicional TablaAdicional)
        {
            if (ModelState.IsValid)
            {
                TablaAdicional.id = Guid.NewGuid();
                db.TablaAdicionals.Add(TablaAdicional);
                db.SaveChanges();
                return Json(new { success = true });
            }

            ViewBag.TablaDatoId = new SelectList(db.TablasDatos, "id", "Descripcion", TablaAdicional.TablaDatoId);
            return PartialView("Create", TablaAdicional);
        }
        #endregion

        #region GET: /TablaAdicional/Edit 
        public ActionResult Edit(Guid? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            TablaAdicional tablaadicional = db.TablaAdicionals.Find(id);
            if (tablaadicional == null)
            {
                return HttpNotFound();
            }
            ViewBag.TablaDatoId = new SelectList(db.TablasDatos, "id", "Descripcion", tablaadicional.TablaDatoId);
            return PartialView("Edit", tablaadicional);
        }
        #endregion

        #region  POST: /TablaAdicional/Edit/5
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit([Bind(Include="id,TablaDatoId,Descripcion,Numero,AdicionalString,AdicionalDecimal,AdicionalBoolean,AdicionalDate")] TablaAdicional tablaadicional)
        {
            if (ModelState.IsValid)
            {
                db.Entry(tablaadicional).State = EntityState.Modified;
                db.SaveChanges();
                return Json(new { success = true });
            }
            ViewBag.TablaDatoId = new SelectList(db.TablasDatos, "id", "Descripcion", tablaadicional.TablaDatoId);
            return PartialView("Edit", tablaadicional);
        }
        #endregion

        #region  GET: /TablaAdicional/Delete/5
        public ActionResult Delete(Guid? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            TablaAdicional tablaadicional = db.TablaAdicionals.Find(id);
            if (tablaadicional == null)
            {
                return HttpNotFound();
            }
            return View(tablaadicional);
        }
        #endregion

        #region  POST: /TablaAdicional/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(Guid id)
        {
            TablaAdicional tablaadicional = db.TablaAdicionals.Find(id);
            db.TablaAdicionals.Remove(tablaadicional);
            db.SaveChanges();
            return RedirectToAction("Index");
        }
        #endregion

        #region Dispose
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }
        #endregion
    }
}
